package com.start_eternity.oasissafewalk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.start_eternity.oasissafewalk.mapper.DriverMapper;
import com.start_eternity.oasissafewalk.pojo.Driver;
import com.start_eternity.oasissafewalk.pojo.Logg;
import com.start_eternity.oasissafewalk.pojo.Notice;
import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.service.DriverService;
import com.start_eternity.oasissafewalk.service.LoggService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DriverServiceImpl implements DriverService {

    @Autowired
    private LoggService loggService;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private DriverMapper driverMapper;
    @Override
    public PageBean page(Integer page, Integer pageSize, String driverName, Short gender,
                         LocalDate begin, LocalDate end) {
        //用logg记录
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());//记录执行的当前时间
        //获取请求头中的jwt令牌, 解析令牌
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String) claims.get("username");
        logg.setLoggName(name);//记录执行者的名字
        logg.setLoggDescription("执行了查询司机的操作");//记录执行的具体操作
        loggService.insert(logg);//调用loggService接口将logg类的信息记录到数据库里面

        //1.设置分页参数
        PageHelper.startPage(page,pageSize);

        //2.执行查询
        List<Driver> driverList = driverMapper.list(driverName, gender, begin, end);
        Page<Driver> p=(Page<Driver>)driverList;
        //3.封装PageBean对象
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        //用logg记录
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());//记录执行的当前时间
        //获取请求头中的jwt令牌, 解析令牌
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String) claims.get("username");
        logg.setLoggName(name);//记录执行者的名字
        logg.setLoggDescription("执行了删除司机的操作，此次删除的司机id为:"+ ids);//记录执行的具体操作
        loggService.insert(logg);//调用loggService接口将logg类的信息记录到数据库里面


        driverMapper.delete(ids);
    }

    @Override
    public void save(Driver driver) {
        //用logg记录
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());//记录执行的当前时间
        //获取请求头中的jwt令牌, 解析令牌
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String) claims.get("username");
        logg.setLoggName(name);//记录执行者的名字
        logg.setLoggDescription("执行了添加司机的操作，此次添加的司机名称为:"+ driver.getDriverName());//记录执行的具体操作
        loggService.insert(logg);//调用loggService接口将logg类的信息记录到数据库里面

       driver.setEntryTime(LocalDate.now());
        driver.setUpdateTime(LocalDateTime.now());
        driverMapper.insert(driver);
    }

    @Override
    public Driver getById(Integer driverId) {
        return driverMapper.getById(driverId);
    }

    @Override
    public void update(Driver driver) {

        //用logg记录
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());//记录执行的当前时间
        //获取请求头中的jwt令牌, 解析令牌
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String) claims.get("username");
        logg.setLoggName(name);//记录执行者的名字
        logg.setLoggDescription("执行了修改司机的操作，此次修改的司机id为:"+ driver.getDriverId());//记录执行的具体操作
        loggService.insert(logg);//调用loggService接口将logg类的信息记录到数据库里面

        driver.setUpdateTime(LocalDateTime.now());
        driverMapper.update(driver);
    }

    @Override
    public List<Driver> queryListByPId(Integer driverId) {
        List<Driver> drivers = driverMapper.queryListByPId(driverId);
        return drivers;
    }

    @Override
    public List<Driver> getMessageById(Integer driverId) {
        List<Driver> drivers = driverMapper.getMessageById(driverId);
        return drivers;
    }

    @Override
    public void addNotice(Notice notice) {
        notice.setNPublishtime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());
       driverMapper.addNotice(notice);
    }
}
